package com.dtruth.dataflow.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

/**
 * logback customizing logger factory
 * @author cherokee
 */
public final class MyLoggerFactory {
    static boolean isload = false;
    static private final Object lock = new Object();
    /**
     * get logger
     * @param name
     * @return 
     */
    public static Logger getLogger(String name) {
        if(!isload){
            synchronized(lock){
                if(!isload){
                    System.setProperty("logback.configurationFile", "mylogback.xml");
                    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
                    try {
                      JoranConfigurator configurator = new JoranConfigurator();
                      configurator.setContext(context);
                      // Call context.reset() to clear any previous configuration, e.g. default 
                      // configuration. For multi-step configuration, omit calling context.reset().
                      context.reset(); 
                      configurator.doConfigure(MyLoggerFactory.class.getResource("/mylogback.xml"));
                      isload = true;
                    } catch (JoranException je) {
                      // StatusPrinter will handle this
                    }
                    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
                }
            }
        }
        return LoggerFactory.getLogger(name);
    }
    /**
     * get logger
     * @param clazz
     * @return 
     */
    public static Logger getLogger(Class clazz) {
        return getLogger(clazz.getName());
    }
}
